package cn.iocoder.yudao.module.biz.controller.admin.expand.powerAccount;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import cn.iocoder.yudao.module.biz.controller.admin.expand.powerAccount.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.expand.powerAccount.ExpandTargetPowerAccountDO;
import cn.iocoder.yudao.module.biz.service.expand.powerAccount.ExpandTargetPowerAccountService;

@Tag(name = "管理后台 - 拓展目标_客户_用电户号")
@RestController
@RequestMapping("/san/biz-expand-target-power-account")
@Validated
public class ExpandTargetPowerAccountController {

    @Resource
    private ExpandTargetPowerAccountService expandTargetPowerAccountService;

    @PostMapping("/create")
    @Operation(summary = "创建拓展目标_客户_用电户号")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-power-account:create')")
    public CommonResult<Long> createExpandTargetPowerAccount(@Valid @RequestBody ExpandTargetPowerAccountSaveReqVO createReqVO) {
        createReqVO.setOwnerUserId(getLoginUserId());
        return success(expandTargetPowerAccountService.createExpandTargetPowerAccount(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新拓展目标_客户_用电户号")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-power-account:update')")
    public CommonResult<Boolean> updateExpandTargetPowerAccount(@Valid @RequestBody ExpandTargetPowerAccountSaveReqVO updateReqVO) {
        expandTargetPowerAccountService.updateExpandTargetPowerAccount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除拓展目标_客户_用电户号")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-power-account:delete')")
    public CommonResult<Boolean> deleteExpandTargetPowerAccount(@RequestParam("id") Long id) {
        expandTargetPowerAccountService.deleteExpandTargetPowerAccount(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除拓展目标_客户_用电户号")
                @PreAuthorize("@ss.hasPermission('san:biz-expand-target-power-account:delete')")
    public CommonResult<Boolean> deleteExpandTargetPowerAccountList(@RequestParam("ids") List<Long> ids) {
        expandTargetPowerAccountService.deleteExpandTargetPowerAccountListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得拓展目标_客户_用电户号")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-power-account:query')")
    public CommonResult<ExpandTargetPowerAccountRespVO> getExpandTargetPowerAccount(@RequestParam("id") Long id) {
        ExpandTargetPowerAccountDO expandTargetPowerAccount = expandTargetPowerAccountService.getExpandTargetPowerAccount(id);
        return success(BeanUtils.toBean(expandTargetPowerAccount, ExpandTargetPowerAccountRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得拓展目标_客户_用电户号分页")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-power-account:query')")
    public CommonResult<PageResult<ExpandTargetPowerAccountRespVO>> getExpandTargetPowerAccountPage(@Valid ExpandTargetPowerAccountPageReqVO pageReqVO) {
        PageResult<ExpandTargetPowerAccountDO> pageResult = expandTargetPowerAccountService.getExpandTargetPowerAccountPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ExpandTargetPowerAccountRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出拓展目标_客户_用电户号 Excel")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-power-account:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExpandTargetPowerAccountExcel(@Valid ExpandTargetPowerAccountPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ExpandTargetPowerAccountDO> list = expandTargetPowerAccountService.getExpandTargetPowerAccountPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "拓展目标_客户_用电户号.xls", "数据", ExpandTargetPowerAccountRespVO.class,
                        BeanUtils.toBean(list, ExpandTargetPowerAccountRespVO.class));
    }

}